#include "../comm.h"
class Solution {
public:
    int n;
    // using PII = pair<int, int>;
    vector<int> nextGreaterElements(vector<int>& nums) {
        n = nums.size();
        vector<int> st;
        vector<int> ans(n);
        for(int i = n - 1; i >= 0; --i)
        {
            int num = nums[i];
            while(st.size() && st.back() <= num) st.pop_back();
            st.emplace_back(nums[i]);
        }

        for(int i = n - 1; i >= 0; --i)
        {
            int num = nums[i];
            while(st.size() && st.back() <= num) st.pop_back();
            if(st.size()) ans[i] = st.back();
            else ans[i] = -1;
            st.push_back(num);
        }
        return ans;
    }
};